<template>
  <div :class="['message-container', type]">
    <div v-if="type === 'bot'" class="message-avatar bot">
      <span>AI</span>
    </div>
    <div v-if="type === 'user'" class="message-avatar user">
      <span>You</span>
    </div>
    <div class="message-content-wrapper">
      <div :class="['message-bubble', type]">
        <div class="message-content" v-html="formattedContent"></div>
      </div>
    </div>
  </div>
</template>

<script>
import { marked } from 'marked';
import { boldifyKeywords } from '../services/api';

export default {
  name: 'ChatMessage',
  props: {
    content: {
      type: String,
      required: true
    },
    type: {
      type: String,
      required: true,
      validator: value => ['user', 'bot', 'separator'].includes(value)
    }
  },
  computed: {
    formattedContent() {
      if (this.type === 'separator') {
        return `<div class="separator">${this.content}</div>`;
      }
      
      // 对于机器人消息，先自动加粗关键词，然后解析Markdown
      let processedContent = this.content;
      if (this.type === 'bot') {
        processedContent = boldifyKeywords(processedContent);
      }
      
      // 解析markdown
      return marked.parse(processedContent);
    }
  },
  watch: {
    // 监听内容变化，通知父组件内容已更新
    content: {
      handler(newContent, oldContent) {
        // 仅当内容真正变化且是机器人消息时通知
        if (newContent !== oldContent && this.type === 'bot') {
          this.$nextTick(() => {
            this.$emit('content-updated');
          });
        }
      },
      immediate: false
    }
  }
}
</script>

<style scoped>
.message-container {
  display: flex;
  padding: 0.5rem 0;
  margin-bottom: 0.5rem;
  position: relative;
}

.message-container.separator {
  justify-content: center;
  margin: 1rem 0;
}

.message-avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  margin-right: 10px;
  font-size: 0.75rem;
  font-weight: 600;
}

.message-avatar.bot {
  background-color: #10a37f;
  color: white;
}

.message-avatar.user {
  background-color: #3b81f6;
  color: white;
}

.message-content-wrapper {
  flex: 1;
  max-width: calc(100% - 40px);
}

.message-bubble {
  border-radius: 6px;
  padding: 0.8rem 1rem;
  width: fit-content;
  max-width: 100%;
}

.message-bubble.bot {
  background-color: #f7f7f8;
  color: #374151;
}

.message-bubble.user {
  background-color: #eff6ff;
  color: #1f2937;
}

.message-container.user {
  flex-direction: row-reverse;
}

.message-container.user .message-avatar {
  margin-right: 0;
  margin-left: 10px;
}

.message-container.user .message-bubble {
  margin-left: auto;
}

.message-content {
  line-height: 1.5;
  word-break: break-word;
}

/* Markdown样式 */
.message-content :deep(p) {
  margin-bottom: 0.7rem;
}

.message-content :deep(p:last-child) {
  margin-bottom: 0;
}

.message-content :deep(ul),
.message-content :deep(ol) {
  margin-left: 1.5rem;
  margin-bottom: 0.8rem;
}

.message-content :deep(li) {
  margin-bottom: 0.3rem;
}

.message-content :deep(code) {
  background-color: rgba(0, 0, 0, 0.05);
  padding: 2px 4px;
  border-radius: 3px;
  font-family: monospace;
  font-size: 0.9em;
}

.message-content :deep(pre) {
  background-color: rgba(0, 0, 0, 0.05);
  padding: 1rem;
  border-radius: 4px;
  overflow-x: auto;
  margin: 0.8rem 0;
}

.message-content :deep(pre code) {
  background-color: transparent;
  padding: 0;
}

.separator {
  background-color: rgba(0, 0, 0, 0.05);
  color: #888;
  padding: 4px 12px;
  border-radius: 10px;
  font-size: 0.8rem;
  text-align: center;
}
</style> 